package com.caiyigo.baihua.serviceImpl;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.caiyigo.baihua.dao.SystemUserMapper;
import com.caiyigo.baihua.dao.SystemUserMapperSql;
import com.caiyigo.baihua.dao.SystemUserRoleMapper;
import com.caiyigo.baihua.dto.UserDto;
import com.caiyigo.baihua.model.SystemUser;
import com.caiyigo.baihua.model.SystemUserRole;
import com.caiyigo.baihua.service.UserService;
import com.caiyigo.baihua.shiro.ShiroCache;
import com.github.miemiedev.mybatis.paginator.domain.PageBounds;

/**
 * @author caihaihui
 * 
 */
@Transactional
@Service
public class UserServiceImpl implements UserService {
	@Resource(name = "systemUserMapper")
	SystemUserMapper userMapper;
	@Autowired
	SystemUserRoleMapper userRoleMapper;
	@Resource(name = "systemUserMapperSql")
	SystemUserMapperSql systemUserMapperSql;

	@Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED)
	@Override
	public int deleteByPrimaryKey(Integer id) {

		return userMapper.deleteByPrimaryKey(id);
	}

	@Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED)
	@Override
	public int insert(SystemUser record) {

		return userMapper.insert(record);
	}

	@Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED)
	@Override
	public int insertSelective(SystemUser record) {

		return userMapper.insertSelective(record);
	}

	@Override
	public SystemUser selectByPrimaryKey(Integer id) {

		return userMapper.selectByPrimaryKey(id);
	}

	@Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED)
	@Override
	public int updateByPrimaryKeySelective(SystemUser record) {

		return userMapper.updateByPrimaryKeySelective(record);
	}

	@Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED)
	@Override
	public int updateByPrimaryKey(SystemUser record) {

		return userMapper.updateByPrimaryKey(record);
	}

	@Override
	public List<SystemUser> list() {

		return userMapper.list();
	}

	@Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED)
	@Override
	public int changeStaus(Integer id, Integer status) {
		SystemUser user = new SystemUser();
		user.setId(id);
		user.setStatus(status);
		return userMapper.updateByPrimaryKeySelective(user);
	}

	@Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED)
	public boolean batchDelete(String[] ids) {
		if (ids.length == 0)
			return false;
		return userMapper.batchDelete(ids) > 0;

	}

	@Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED)
	public boolean batchGrant(Integer[] role_ids, String uids) {
		String[] ids = uids.split(",");
		boolean result = userRoleMapper.batchDeleteByUserid(ids) >= 0;

		if (role_ids == null)
			return result;

		List<SystemUserRole> list = new ArrayList<SystemUserRole>();
		String[] idArray = uids.split(",");
		for (int j = 0; j < idArray.length; j++) {
			for (Integer r : role_ids) {
				list.add(new SystemUserRole(Integer.valueOf(idArray[j]), r));
			}
		}
		result = userRoleMapper.batchInsert(list) > 0;
		ShiroCache.clearAuthorizationInfoAll();
		return result;
	}

	@Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED)
	public boolean grant(Integer[] role_ids, Integer userId) {
		boolean result = userRoleMapper.deleteByUserid(userId) >= 0;

		if (role_ids == null)
			return result;
		List<SystemUserRole> list = new ArrayList<SystemUserRole>();
		for (Integer r : role_ids) {
			list.add(new SystemUserRole(userId, r));
		}
		result = userRoleMapper.batchInsert(list) > 0;
		ShiroCache.clearAuthorizationInfoAll();

		return result;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.caiyigo.zoo.service.UserService#selectByName(java.lang.String)
	 */
	@Override
	public SystemUser selectByName(String name) {
		// TODO Auto-generated method stub
		return userMapper.selectByName(name);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.caiyigo.zoo.service.UserService#getUserList(com.github.miemiedev.
	 * mybatis.paginator.domain.PageBounds)
	 */
	@Override
	public List getUserList(UserDto user, PageBounds pageBounds) {

		return systemUserMapperSql.getUserList(user, pageBounds);
	}

}
